

/*let promise1 = new Promise((resolve, reject) => {
  setTimeout(() => {
    // resolve('hello')
    reject(new Error())
  }, 1000)
}).then((res) => {
  console.log(res)
}, (err) => {
  console.log(err)
})*/
/*.catch((err) => {
  console.log(err)
})*/

// console.log(promise1)

// const myAjax = () => {
//   return new Promise((resolve, reject) => {
//     resolve(123)
    /*$.ajax({
      type: 'GET',
      url: 'https://gank.io/api/v2/data/category/Girl/type/Girl/page/1/count/10',
      success(res) {
        resolve(res)
      },
      fail(err) {
        debugger
        reject(err)
      }
    })*/
  // })
// }

/*const myAjax1 = () => {
  return new Promise((resolve, reject) => {
    reject(new Error())
  })
}

const myAjax2 = () => {
  return new Promise((resolve, reject) => {
    resolve(123)
  })
}

Promise.all([myAjax(), myAjax1(), myAjax2()])
  .then((res) => {
    console.log(res)
    console.log('success')
  })
  .catch((err) => {
    console.log(err)
    console.log('fail')
  })
  .finally(() => {
    console.log('============')
  })*/



/*myAjax().then((res) => {
  console.log(res)
}, (err) => {
  console.log(err)
})*/

// 回调地狱

/*myAjax().then((res) => {
  // 成功
  console.log(res)

}).catch((err) => {
  // 失败
  console.log(err)

}).finally(() => {
  // 完成
  console.log('finally')
})*/

// Promise.reject()
// Promise.resolve()


const axios = (option = {}) => {
  return new Promise((resolve, reject) => {
    $.ajax({
      type: option.method || 'GET',
      url: option.url,
      data: option.data || {},
      success(res) {
        resolve(res)
      },
      error(err) {
        reject(err)
      }
    })
  })
}









let currentPage = 1
let pageNumber = 10
let hasNextPage = true

const getList = (option = {}) => {
  const { page = 1, count = 10 } = option
  $('.loading').addClass('show')

  axios({
    type: 'GET',
    url: `https://gank.io/api/v2/data/category/Girl/type/Girl/page/${page}/count/${count}`
  }).then((res) => {
    const {data, page} = res;
    currentPage = page
    $('.list').html('')
    if (data.length > 0) {
      if (data.length < pageNumber) {
        hasNextPage = false
      } else {
        hasNextPage = true
      }
      data.map(item => {
        const {_id, title, author, views, desc, url: posterUrl} = item
        $(`<section class="item">
            <a class="title" href="javascript:void(0)" data-id="${_id}">
                ${title}
                <div>
                    <span class="author">${author}</span>
                    <span class="views">
                        <i class="fa fa-eye"></i>
                        ${views}
                    </span>
                </div>
            </a>
            <div class="poster" style="background-image: url(${posterUrl})" >
                <div class="desc">
                    ${desc}
                </div>
            </div>
        </section>`).appendTo('.list');
      })
    } else {
      $('.list').append('<section class="empty">暂无数据</section>')
    }
  }).catch((err) => {
    console.log(err)
  }).finally(() => {
    $('.loading').removeClass('show')
  })





  /*$.ajax({
    url: `https://gank.io/api/v2/data/category/Girl/type/Girl/page/${page}/count/${count}`,
    success(res) {
      const {data, page} = res;
      currentPage = page
      $('.list').html('')

      if (data.length > 0) {

        if (data.length < pageNumber) {
          hasNextPage = false
        } else {
          hasNextPage = true
        }

        data.map(item => {

          const {_id, title, author, views, desc, url: posterUrl} = item

          $(`
          <section class="item">
            <a class="title" href="javascript:void(0)" data-id="${_id}">
                ${title}
                <div>
                    <span class="author">${author}</span>
                    <span class="views">
                        <i class="fa fa-eye"></i>
                        ${views}
                    </span>
                </div>
            </a>
            <div class="poster" style="background-image: url(${posterUrl})" >
                <div class="desc">
                    ${desc}
                </div>
            </div>
        </section>
          `).appendTo('.list');
        })
      } else {
        $('.list').append('<section class="empty">暂无数据</section>')
      }
      $('.loading').removeClass('show')
    }
  })*/
}


getList({
  page: currentPage,
  count: pageNumber
})


$('.pager').on('click', '.prev', function () {
  if (currentPage > 1) {
    currentPage -= 1;
    getList({
      page: currentPage,
      count: pageNumber
    })
  } else {
    //
  }
})

$('.pager').on('click', '.next', function () {
  // console.log(123)

  if (hasNextPage) {
    currentPage += 1;
    getList({
      page: currentPage,
      count: pageNumber
    })
  } else {
    //
  }
})

// console.log($('.item a'))


// 事件委托

$('.list').on('click', '.item a', function () {
  // console.log($(this).data('id'))

  const id = $(this).data('id')

  $.ajax({
    url: `https://gank.io/api/v2/post/${id}`,
    success(res) {
      console.log(res)
    }
  })


  // location.href = 'show.html?id=' + $(this).data('id')

})


